﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.Utils;
using Jungle.Code.Core;
using System.Collections.Generic;
using Jungle.Code.BLL;

namespace Jungle.Code.BLL
{
	public partial class PageBuilder : Page
	{
		#region Fields
		private Node currentNode;
		#endregion

		protected PageBuilder() {
			currentNode = new Node();
		}

		protected override void OnInit(EventArgs e) {
			//Info();

            BaseUserModule.virt.Clear();
			string[] virtuals = UrlHelper.GetVirtuals();

			if (virtuals.Length == 1 && virtuals[0] == "default") 
            {
                if (Request.QueryString["RequestUrl"] == null)
                    Response.Redirect(UrlHelper.GetSiteUrl() + "/ru/main.aspx");

                //Load node by url - http://jungle.net.ua/default.aspx?RequestUrl=ru/news - usualy PostBack
                string ReqUrl = Request.QueryString["RequestUrl"];

                if (ReqUrl.EndsWith("/main"))
                {
                    ReqUrl = ReqUrl.Substring(0, ReqUrl.IndexOf("/main"));
                }

                Node item = new Node();
                item = Node.GetByUrl(ReqUrl);

                if (item != null)
                {
                    this.currentNode = item;
                }
                string[] virtuals2 = ReqUrl.Split('/');
                foreach (string s in virtuals2)
                    BaseUserModule.virt.Add(s);
			}

            else {
				//Load node by url - http://jungle.net.ua/ru/news.aspx
                string url = "";
				foreach(string v in virtuals)
                {
                    url += "/" + v;
                }
                url = url.Substring(1);
                Node item = new Node();
                item = Node.GetByUrl(url);

                if (item != null)
                {
                    this.currentNode = item;
                }
                foreach(string s in virtuals)
                    BaseUserModule.virt.Add(s);

            }

			LoadContent();
			base.OnInit(e);
		}

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
        }

		public void LoadContent() {
			Template tempData = new Template();
			tempData = Template.GetByTemplateID(currentNode.TemplateID);
			string fullTemplatePath = UrlHelper.GetApplicationPath() + tempData.BasePath + tempData.UserTemplate;
			BaseTemplate template = (BaseTemplate)base.LoadControl(fullTemplatePath);
			template.Title = currentNode.Title;
            template.Keywords = currentNode.Keywords;
            template.Description = currentNode.Description;



			List<Section> sections = new List<Section>();
			sections = Section.GetByNodeID(currentNode.NodeID);
			foreach (Section sect in sections) {
				if (sect.Enabled) {
					Module moduleData = new Module();
					moduleData = Module.GetByModuleID(sect.ModuleID);
					Control controlModule = base.LoadControl(UrlHelper.GetApplicationPath() + moduleData.BasePath + moduleData.UserControlFile);
					BaseUserModule module = (BaseUserModule)controlModule;
					module.currentNode = this.currentNode;
					module.section = sect;
					if (template.Containers.ContainsKey(sect.Container)) {
						((PlaceHolder)template.Containers[sect.Container]).Controls.AddAt(0, module);
					}
				}
			}
			this.Controls.AddAt(0, template);
		}

		public void Info() {
			base.Response.Write("SiteUrl : " + UrlHelper.GetSiteUrl());
			base.Response.Write("<br>ApplicationPath : " + UrlHelper.GetApplicationPath());
			base.Response.Write("<br>RequestUrl : " + UrlHelper.GetRequestUrl());
			base.Response.Write("<br>VirtualUrl : " + HttpContext.Current.Items["VirtualUrl"]);
			string[] textArray1 = UrlHelper.GetVirtuals();
			base.Response.Write("<br>Virtuals : ");
			if (textArray1 != null) {
				for (int i = 0; i < textArray1.Length; i++) {
					base.Response.Write("<br>" + textArray1[i]);
				}
			}
		}
	}
}
